﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
using WPFExcelReport;
namespace INTEX.SCHEDULER
{
    public partial class ITXSSPA
    {
        public void ResetStdMachine()
        { 
            //AMMANDANT AND  AMKSTKRS=:AMKSTKRS AND AMKST
            INTEXDBC.DeleteICC_STDMACHINE(null,1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
        }
         
        public DataSet JLTPLANDB(object _KSTKRS = null, object _KST = null, object _ABFAGANG = null, object p4 = null, object p5 = null)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwData = null;
            string SetDate = "";
            try
            {
                VICC_PVKPOS VKPK = new VICC_PVKPOS();
                dwData = VKPK.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                INTEXDBC.InsertICC_PVKPOS(dwData, 1);//INSERT//织造 

                dwData = VKPK.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                INTEXDBC.UpdateICC_PVKPOS(dwData, 11);//更新销售单信息  
                INTEXDBC.UpdateICC_PVKPOS(dwData, 21);//订单行汇总                   

                dwData = TBMain.NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFKSTKRS"] = (string)_KSTKRS;
                dwData["ABFKST"] = (string)_KST;
                dwData["MKFERLKZ"] = " ";
                dwData["ABFAGANG"] = (string)_ABFAGANG;
                //INTEXDBC.DeleteMKRF(dwData, 2);
                //INTEXDBC.DeleteICC_MKRF(dwData, 2); 
                INTEXDBC.InsertICC_MKRF(dwData, 4);//插入所有生产单进入待排产的记录
                INTEXDBC.InsertICC_MKRF(dwData, 5);//插入所有销售进入待排产的记录
                //INTEXDBC.UpdateICC_MKRF(dwData, 7);//MACHINEID
                INTEXDBC.UpdateICC_MKRF(dwData, 9);//ABPEALFM,ABPEAKG,ABPERLDAT,ABPERLKZ 
                dwData["C3"] = 713;
                INTEXDBC.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8
                dwData["C3"] = 733;
                INTEXDBC.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8
                dwData["C3"] = 723;
                INTEXDBC.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8
                dwData["C3"] = 610;
                INTEXDBC.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8
                dwData["C3"] = 733;
                INTEXDBC.UpdateICC_MKRF(dwData, 12);//BAMTR1....BAMTR7
                dwData["WDTYP"] = "B";
                INTEXDBC.UpdateICC_MKRF(dwData, 13);//WDECK....BAMTR7
                INTEXDBC.UpdateICC_MKRF(dwData, 10);//ABARESMG
                INTEXDBC.UpdateICC_MKRF(dwData, 11);//ABFISTARTDAT,ABFISTARTZT,ABFIENDDAT,ABFIENDZT    

            }
            catch (Exception ex)
            {
                LogHelper.error(ex.StackTrace + ex.Message);
            }
            return Return;
        }
        public DataSet JLTAPSDB(object _KSTKRS = null, object _KST = null, object _ABFAGANG = null, object p4 = null, object p5 = null)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwData = null;
            string SetDate = "";
            try
            {
                VICC_PVKPOS VKPK = new VICC_PVKPOS();
                dwData = VKPK.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                INTEXDBC.InsertICC_PVKPOS(dwData, 1);//INSERT
                INTEXDBC.UpdateICC_PVKPOS(dwData, 2);//VKKLINES
                INTEXDBC.UpdateICC_PVKPOS(dwData, 3);//JNUM
                INTEXDBC.UpdateICC_PVKPOS(dwData, 4);//JNUM
                INTEXDBC.UpdateICC_PVKPOS(dwData, 21);//JNUM

                INTEXDBC.InsertICC_JWYARN(dwData, 1);//J
                INTEXDBC.InsertICC_JWYARN(dwData, 2);//W
                INTEXDBC.UpdateICC_JWYARN(dwData, 2);//RGB

                dwData = TBMain.NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFKSTKRS"] = (string)_KSTKRS;
                dwData["ABFKST"] = (string)_KST;
                dwData["MKFERLKZ"] = " ";
                dwData["ABFAGANG"] = (string)_ABFAGANG;
                //INTEXDBC.DeleteMKRF(dwData, 2);
                //INTEXDBC.DeleteICC_MKRF(dwData, 2); 
                INTEXDBC.InsertICC_MKRF(dwData, 1);
                INTEXDBC.UpdateICC_MKRF(dwData, 4);//更新纱支等信息
                INTEXDBC.UpdateICC_MKRF(dwData, 5);//数量更新
            }
            catch (Exception ex)
            {
                LogHelper.error(ex.StackTrace + ex.Message);
            }
            return Return;
        }
        public DataSet DBSchedulePrepare(DataSet db)
        {
            DataSet SetProd = new DataSet();
            try
            {
                DataRow[] rows;
                DataTable tb;
                SetProd.Clear();
                rows = db.Tables["ICC_MKRF"].Select("(ABFSTATUS='R' OR ABFISTARTDAT>0) OR SPCONFIRM='YES     '");//已经开始或生产//手工排产
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_MKRF";
                    SetProd.Merge(tb);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error(ex.StackTrace + ex.Message);
            }
            return SetProd;
        }
     
        public void RefreshJobDB( string KRS, string LFD, string POS,DataSet SetData, DataSet SetEnv, DataSet SetVKK, DataSet SetVKP, DataSet SetProd)
        {
            DataRow dwJob = null;
            string filter = "ABFAUFKRS='" + KRS + "' AND ABFAUFLFD='" + LFD + "' AND ABFAUFPOS='" + POS + "'";
            DataRow[] rows = SetData.Tables["ICC_MKRF"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwJob = row;
            }
            if (dwJob == null)
                return;
            DataTable tb;
            EvaluateMachine(dwJob);
            filter = "VKPAUFKRS='" + dwJob["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwJob["VKPAUFLFD"].ToString() + "'";
            rows = SetData.Tables["ICC_MKRF"].Select(filter);
            if (rows.Length > 0)
            {
                tb = rows.CopyToDataTable();
                tb.TableName = "VKKMKRF";
                if (SetVKK.Tables.Contains("VKKMKRF"))
                {
                    SetVKK.Tables["VKKMKRF"].Clear();
                }
                SetVKK.Merge(tb);
            }
            filter = "VKPAUFKRS='" + dwJob["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwJob["VKPAUFLFD"].ToString() + "' AND VKPAUFPOS='" + dwJob["VKPAUFPOS"].ToString() + "'";
            rows = SetData.Tables["ICC_MKRF"].Select(filter);
            if (rows.Length > 0)
            {
                tb = rows.CopyToDataTable();
                tb.TableName = "VKPMKRF";
                if (SetVKP.Tables.Contains("VKPMKRF"))
                {
                    SetVKP.Tables["VKPMKRF"].Clear();
                }
                SetVKP.Merge(tb);
            }
            filter = "ABPART='" + dwJob["ABPART"].ToString() + "' AND ABPVAR='" + dwJob["ABPVAR"].ToString() + "' AND ABPFARBE='" + dwJob["ABPFARBE"].ToString() + "'";
            if (rows.Length > 0)
            {
                tb = rows.CopyToDataTable();
                tb.TableName = "FMKRF";
                if (SetProd.Tables.Contains("FMKRF"))
                {
                    SetProd.Tables["FMKRF"].Clear();
                }
                SetProd.Merge(tb);
            }
        }
    }
}

